
import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import BaseLayer from "../layer/Baselayer";
import AudioManager from "../manager/AudioManager";
import ToastManager from "../manager/ToastManager";
import { StaticInstance } from "../StaticInstance";
import { BytePlantform}  from "../Tool/BytePlantform";
import { ReactiveAutoSave } from "../Tool/ReactiveAutoSave"
const { ccclass, property } = cc._decorator;



@ccclass('slider_page')
export class slider_page extends BaseLayer {
    mask: cc.Node = null;
    frame: cc.Node = null;
    btn_label: cc.Label =null; 
    slider_button: cc.Node = null;
    btnClose: cc.Node = null

    private static _ins: slider_page | null = null; // 单例实例

    // 单例模式：获取实例
    public static get ins(): slider_page {
        return this._ins;
    }
    protected onLoad(): void {
        this.frame = cc.find('frame', this.node)
        this.btn_label = cc.find('frame/slider_Button/Background/btn_label', this.node).getComponent(cc.Label)
        this.slider_button = cc.find('slider_Button', this.frame)
        this.btnClose = cc.find('btn_close', this.frame)
        this.btnClose.on('click', this.onCloseClick, this)
        this.slider_button.on('click', this.btn_run, this)
        this.frame.scale = 0;   
        this.frame.runAction(cc.scaleTo(0.3, 1).easing(cc.easeIn(2.0)));

    }

    onDestroy() {
        this.btnClose.off('click', this.onCloseClick, this)
        this.slider_button.off('click', this.btn_run, this)
    }

    start() {
         // 设置单例实例
        slider_page._ins = this;
        this.refrush_data();
    
    }
    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.GIFT, false)
    }

    public refrush_data(){
        console.log("refrush_data");
        if(ReactiveAutoSave.ins.getData().gift == 0){
            //0 =default,1=添加了。2=领取奖励
            this.btn_label.string = "跳转侧边栏"
         }else if(ReactiveAutoSave.ins.getData().gift == 1){
            //0 =default,1=添加了。2=领取奖励
            this.btn_label.string = "领取奖励"
         }else{
             this.btn_label.string = "跳转侧边栏"
         }
    }
    
    btn_run(){
        console.log("btn_run");
        if(ReactiveAutoSave.ins.getData().gift == 0){
            this.add_siderbar();
         }else if(ReactiveAutoSave.ins.getData().gift == 1){
            this.get_gift();
         }else{
            this.add_siderbar();
         }
    }

    private get_gift(){
        ReactiveAutoSave.ins.getData().gift = 2;
        this.slider_button.getComponent(cc.Button).interactable = false;
        ToastManager.instance.show('游戏火爆奖励领取成功~', { gravity: 'CENTER', bg_color: cc.color(76, 76, 76, 255) })
    }


   private add_siderbar() {
         // 调用SDK接口
         console.log("开始添加 add_siderbar");
         BytePlantform.ins.navigate_to_douyin((success: boolean) => {
            if (success) {
                // this.node.active = false;
                console.log('add_siderbar 添加侧边栏跳转成功');
                // if(LvLStorage.get_gift()<1){
                //     LvLStorage.record_gift(1);
                //     console.log('add_siderbar 添加侧边栏跳转成功,记录数据 1');
                // }
            } else {
                console.log('add_siderbar 跳转失败');
            }
        });
    }

}


